﻿#include "chooselevelscene.h"
#include<QMenuBar>
#include<QAction>
#include<QPainter>
#include<mypushbutton.h>
#include<QDebug>
#include"mainscene.h"
#include<QMouseEvent>
#include<QLabel>
#include"playscene.h"
#include<QTimer>
#include<QSound>
#pragma execution_character_set("utf-8");
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡场景
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡场景");

    //用代码实现菜单栏创建
    QMenuBar * menuBar = this->menuBar();
    QMenu * menu = menuBar->addMenu("开始");
    QAction * close = new QAction("关闭");
    menu->addAction(close);
    this->setMenuBar(menuBar);

    //关闭按钮实现功能
    connect(close,&QAction::triggered,[=](){
        this->close();
    });

    QSound * backSound = new QSound(":/res/BackButtonSound.wav");
    //返回按钮
   MyPushButton * backBtn =
            new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
   backBtn->setParent(this);
   backBtn->move(this->width() - backBtn->width(),this->height()-backBtn->height());


   //点击返回
   connect(backBtn,&MyPushButton::clicked,[=](){
//       qDebug() << "你点击了返回按钮";
       backSound->play();

       //延时返回到主场景中
       QTimer::singleShot(500,this,[=]{
       //告诉主场景 我返回了 主场景监听
       emit this->chooseSceneBack();
       });
   });



    QSound * chooseSound = new QSound(":/res/TapButtonSound.wav");

   //创建选择关卡的按钮
   for(int i =0;i<20;i++)
   {
       MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
       menuBtn->setParent(this);
       menuBtn->move(25+i%4*70,130+i/4*70);


       //监听每个按钮的点击事件
       connect(menuBtn,&MyPushButton::clicked,this,[=](){
           //音效
           chooseSound->play();

           QString str = QString("您选择的是第 %1 关").arg(i+1);
           qDebug() << str;

           //进入到游戏场景
           this->hide();//将选关场景隐藏掉
           play = new PlayScene(i+1);//创建游戏场景
           play->setGeometry(this->geometry());//设置游戏界面窗口位置
           play->show();

           //监听playScene场景的返回按钮
           connect(play,&PlayScene::playSceneBack,this,[=](){
                this->show();
                delete play;
                play = NULL;
                this->show();

           });
       });



       QLabel * label = new QLabel;
       label->setParent(this);
       label->setFixedSize(menuBtn->width(),menuBtn->height());
       label->setText(QString::number(i+1));
       label->move(25+i%4*70,130+i/4*70);
       //设置label上的文字对齐方式 水平居中和垂直居中
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
       //设置让鼠标进行穿透 51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
   }

}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;

    //加载背景
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width(),pix.height());
    painter.drawPixmap(10,30,pix);





}
